﻿/*  
 *  IIS LogFile Analyser, Copyright (C) 2013. http://www.iis-analyser.com
 *  --
 *  This program is free software, provided "AS IS" WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
 * 
 *  File Author: Ian Quigley,    2013-02-23
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;

namespace Web.DisplayView
{
    public class BarChartView : IDisplayView
    {
        public BarChartView()
        {}

        public string Name()
        {
            return "Simple Bar Chart";
        }

        public string ToHtml(DataSet dataset)
        {
            long min = int.MaxValue;
            long max = int.MinValue;
            foreach (DataRow row in dataset.Tables[0].Rows)
            {
                if ((long)row[1] < min)
                    min = (long)row[1];

                if ((long)row[1] > max)
                    max = (long)row[1];
            }

            long delta = max - min;

            StringBuilder html = new StringBuilder();

            html.Append( "<table cellspacing='0' width='100%'><tr><thead><td>"  + dataset.Tables[0].Columns[0].Caption + "</td><td>" + dataset.Tables[0].Columns[1].Caption + "</td><td style='width:80%'></td></tr></thead>");
            
            foreach (DataRow row in dataset.Tables[0].Rows)
            {
                html.AppendFormat( "<tr><td>{0}</td><td>{1}</td>", row[0], row[1] );
                
                long value = (long)row[1];
                html.AppendFormat( "<td><div style='width: " + ((value - min) * 100) / delta + "%; background: #f00; font-size:0.1em;'>&nbsp;</div></td>" );

                html.Append( "</tr>" );
            }

            html.Append( "</table>" );
            return html.ToString();
        }
    }
}